import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createBinaryTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;

        B.left = D;
        B.right = E;

        C.left = F;
        C.right = G;

        E.right = H;

        return A;
    }
    /**
     根据二叉树创建字符串
     1.判断是否为null,为null返回stringBuilder
     2.判断左右
     */
    public String tree2str(TreeNode root) {
        StringBuilder stringBuilder = new StringBuilder();
        if(root == null) {
            return stringBuilder.toString();
        }
        tree2strChild(root,stringBuilder);
        return stringBuilder.toString();
    }
    /**
     二叉树转换为字符串
     1.判断是否为null,为null直接返回
     2.判断左子树是否为null,然后再判断左
     左子树不为null,append,递归判左
     左子树为null,右子树为null
     左子树为null,右子树不为null
     3.判断右子树是否为null
     右子树不为null,appen,递归判右
     为null,return
     */
    private void tree2strChild(TreeNode root,StringBuilder stringBuilder) {
        if(root == null) {
            return ;
        }
        stringBuilder.append(root.val);
        //左
        if(root.left != null) {
            stringBuilder.append("(");
            tree2strChild(root.left,stringBuilder);
            stringBuilder.append(")");
        }else {
            if(root.right != null) {
                stringBuilder.append("()");
            }else {
                return;
            }
        }
        //右
        if(root.right != null) {
            stringBuilder.append("(");
            tree2strChild(root.right,stringBuilder);
            stringBuilder.append(")");
        }else {
            return;
        }
    }
    /**
     二叉树的前序遍历
     1.判断是否为null,为null,return list
     2.通过cur来代替root进行移动,不为null,则push + add,为null,弹出元素右侧寻找

     */
    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if(root == null) {
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode tmp = null;

        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                list.add(cur.val);
                stack.push(cur);
                cur = cur.left;
            }
            tmp = stack.pop();
            cur = tmp.right;
        }
        return list;
    }
    /**
     二叉树的中序遍历
     1.判断是否为null
     2.cur不为null,则一直向左寻找,当为null,pop元素,并ada
     3.修改cur指向,继续判断右
     */
    public List<Character> inorderTraversal(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if(root == null) {
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode tmp = null;

        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            tmp = stack.pop();
            list.add(tmp.val);
            cur = tmp.right;
        }
        return list;
    }
}
